<?php
namespace app\ucenter\model;

use think\Model;

class UcenterUsers extends Model
{
    /*以邮箱地址获取用户Uid*/
    public static function checkUidForEmail($email){
        return $uid = self::where(['email'=>$email])->value('uid');
    }
    /*以邮箱地址获取用户信息*/
    public static function checkUserForEmail($email){
        return $user = self::where(['email'=>$email])->find();
    }
    /*以Uid地址获取用户信息*/
    public static function checkUserForUid($uid){
        return $user = self::where(['uid'=>$uid])->find();
    }
    /*判断登陆信息*/
    public static function checkLogin($email,$password,$data=false){
        if(empty($email) || !is_email($email)){
            return ['code'=>-1,'error'=>'The mailbox is not entered correctly.'];
        }
        if(empty($password)){
            return ['code'=>-1,'error'=>'Password is required.'];;
        }
        $user = self::where(['email'=>$email])->find();
        if(empty($user)){
            return ['code'=>-1,'error'=>'Mailbox does not exist.'];;
        }
        if($isSync){
            $user['password'] = $password;
        }
        if($user['password'] != $password){
            return ['code'=>-1,'error'=>'Wrong password.'];;
        }
        $user->last_time = $user->login_time;
        $user->last_ip = $user->login_ip;
        $user->login_time = time();
        $user->login_ip = getIp();
        $user->save();
        return ['code'=>0,'error'=>'','data'=>$user];
    }
    /*判断登陆信息*/
    public static function editUser($data,$uid=null){
        
        if(empty($uid)){
            return ['code'=>-1,'error'=>'Condition does not exist'];
        }
        if(isset($data['email']) && !is_email($data['email'])){
            return ['code'=>-1,'error'=>'The mailbox is not entered correctly.'];
        }
        /*判断是否唯一*/
        if($data['email'] != $data['condition'] && self::checkUidForEmail($data['email'])){
            return ['code'=>-1,'error'=>'The mailbox already exists'];
        }
        if(isset($data['password']) && empty($data['password'])){
            return ['code'=>-1,'error'=>'Password is required.'];;
        }
        if($result = self::where(['uid'=>$uid])->update($data)){
            $apis = config('ucenter_edit');
            if(count($apis) > 0){
                $strind = json_encode(['cmd'=>'updateUserInfo','arg'=>$data]);
                $header = array(
                    'Content-Type: application/json',
                    'Content-Length: ' . strlen($strind),
                );
                foreach ($apis as $key => $url) {
                    $resutl = post_curl($url,$strind,false,$header);
                }
            }
            return ['code'=>0,'error'=>'','data'=>[]];
        }
        return ['code'=>-1,'error'=>'Fail','data'=>[]];
    }
    /*判断登陆信息*/
    public static function register($data){

        if(empty($data['email']) || !is_email($data['email'])){
            return ['code'=>-1,'error'=>'The mailbox is not entered correctly.'];
        }
        /*判断是否唯一*/
        if(self::checkUidForEmail($data['email'])){
            return ['code'=>-1,'error'=>'The mailbox already exists'];
        }
        if(empty($data['password'])){
            return ['code'=>-1,'error'=>'Password is required.'];;
        }
        
        $data['last_time'] = $data['login_time'] = $data['create_time'] = time();
        $data['login_ip'] = $data['last_ip'] = $data['create_ip'] = getIp();
        if($result = self::insert($data)){
            $apis = config('ucenter_register');
            if(count($apis) > 0){
                if(count($apis) > 0){
                    $strind = json_encode(['cmd'=>'register','arg'=>$data]);
                    $header = array(
                        'Content-Type: application/json',
                        'Content-Length: ' . strlen($strind),
                    );
                    foreach ($apis as $key => $url) {
                        $resutl = post_curl($url,$strind,false,$header);
                        file_put_contents("123.logs", $resutl);
                    }
                }
            }
            return ['code'=>0,'error'=>'','data'=>[]];
        }
        return ['code'=>-1,'error'=>'Fail','data'=>[]];
    }
    /*修改密码*/
    public static function editPassword($data){

         $user = self::checkUserForEmail($data['email']);
         if(!$user){
            return ['code'=>-1,'msg'=>lang('User does not exist')];
         }
         if(empty($data['password'])){
            return ['code'=>-1,'msg'=>lang('Please enter your password')];
         }
         $user->password = $data['password'];
         if($user->save()){
            $apis = config('ucenter_edit');
            $data['cmd']='changeUserPwd';
            if(count($apis) > 0){
                $strind = json_encode(['cmd'=>'changeUserPwd','arg'=>$data]);
                $header = array(
                    'Content-Type: application/json',
                    'Content-Length: ' . strlen($strind),
                );
                foreach ($apis as $key => $url) {
                    post_curl($url,$strind,false,$header);
                }
            }
            return ['code'=>0,'msg'=>lang('success')];
         }
         return ['code'=>1,'msg'=>lang('fail')];
    }
    /*同步用户信息*/
    public static function syncUser($data){
        /*判断是否唯一*/
        if($uid = self::checkUidForEmail($data['email'])){
            self::where(['uid'=>$uid])->update($data);
            $apis = config('ucenter_edit');
            $data['cmd']='CMP.DeleteUsers';
            if(count($apis) > 0){
                foreach ($apis as $key => $url) {
                    post_curl($url,$data);
                }
            }
            return ['code'=>0,'error'=>'','data'=>[]];
        }
        $result = self::register($data);
        return $result;
    }
    /*删除用户信息*/
    public static function delUser($email){
        if(self::where('email',$email)->delete()){
            $apis = config('ucenter_apis');
            if(count($apis) > 0){
                foreach ($apis as $key => $url) {
                    post_curl($url,['email'=>$email,'cmd','CMP.deleteChildUser']);
                }
            }
            return true;
        }
        return false;
    }
}